package com.ruoyi.web.controller.tuike;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.VideoTypeEnum;
import com.ruoyi.tiktok.domain.TFile;
import com.ruoyi.tiktok.domain.TFolder;
import com.ruoyi.tiktok.service.impl.TIFileService;
import com.ruoyi.tiktok.service.impl.TFolderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/video-distribute/folders")
public class TFolderController extends BaseController {


    @Autowired
    public  TFolderService tFolderService;
    @Autowired
    private TIFileService tFileService;
    /**
     * 新增文件夹
     */
    //@PreAuthorize("@ss.hasPermi('video-distribute:video-distribute:add')")
    @Log(title = "folderCreate", businessType = BusinessType.INSERT)
    @PostMapping(path = "/add")
    public AjaxResult add(@RequestBody TFolder tFolder)
    {
        return tFolderService.addFold(tFolder);
    }

    /**
     *  文件夹
     */
    //@PreAuthorize("@ss.hasPermi('video-distribute:video-distribute:add')")
    @Log(title = "file list")
    @GetMapping(path = "/list")
    public AjaxResult listFolder(@Param("parentId")Long parentId)
    {
        List<TFolder> selectLIst = tFolderService.selectFolderList(parentId);
        return AjaxResult.success(selectLIst);
    }


    /**
     * 重命名
     * @param id id
     * @param folderName 修改文件夹名称
     * @param type //类型 0 文件夹 1 文件
     */
    @GetMapping("/rename/{id}/{folderName}/{type}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult rename(@PathVariable Long id,
                             @PathVariable String folderName,
                             @PathVariable Integer type) {
        //如果只是一个文件则修改文件的名称
        boolean updated;
        if(type == 0){
            TFolder tFolder = new TFolder();
            tFolder.setFolderId(id);
            tFolder.setFolderName(folderName);
            tFolder.setType(type);
            updated = tFolderService.updateById(tFolder);
        } else {
            TFile tFile = new TFile();
            tFile.setFileId(id);
            String[] fileName = folderName.split("\\.");
            if(fileName.length > 2){
                folderName = folderName + "." + VideoTypeEnum.getType(3).type;
            }
            tFile.setFileName(folderName);

            updated =  (tFileService.updateTFile(tFile) ==1);
        }
        return updated ? success(): error("更新失败请重试");
    }

    //@PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "文件夹 or 文件删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{folderId}/{type}")
    public AjaxResult remove(@PathVariable Long folderId, @PathVariable Long type)
    {
        boolean result;
        if(type ==0){
           result = tFolderService.deleteById(folderId);
        }else {
            result = tFileService.deleteTFileById(folderId) ==1;
        }
        // 不为空才可以伤处
        return result?success():error();
    }
}
